package com.imageflowpro.image_flow_pro.controller.user;

import com.imageflowpro.image_flow_pro.dto.UserUploadImageDTO;
import com.imageflowpro.image_flow_pro.result.Result;
import com.imageflowpro.image_flow_pro.service.IMediaImageService;
import com.imageflowpro.image_flow_pro.vo.UserUploadImageVO;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController("userMediaImageController")
@RequestMapping("/user/minio")
//用户端多媒体接口
public class MediaImageController {

    @Autowired
    private IMediaImageService iMediaImageService;


    //在 Spring MVC 中，@RequestBody 用于处理 JSON 或 XML 格式的请求体，并不适用于文件上传。
    @PostMapping("/userUploadImage")
    public Result<UserUploadImageVO> userUploadImage(
            @RequestParam(value = "minioName",required = false) String minioName,
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "verificationId", required = false) Long verificationId) {

        UserUploadImageDTO userUploadImageDTO = new UserUploadImageDTO();
        userUploadImageDTO.setMinioName(minioName);
        userUploadImageDTO.setFile(file);
        userUploadImageDTO.setId(id);
        userUploadImageDTO.setVerificationId(verificationId);

        return Result.success(iMediaImageService.userUploadImage(userUploadImageDTO));
    }

    @GetMapping("/getImage/{imageId}")
    public void getImageById(HttpServletResponse response, @PathVariable("imageId") Long imageId){
        iMediaImageService.getImageById(response,imageId);
    }

    @DeleteMapping("/deleteImage/{imageId}")
    public Result deleteImageById( @PathVariable("imageId") Long imageId){
        iMediaImageService.deleteImageById(imageId);
        return Result.success();
    }

    @GetMapping("/downloadImage/{orderId}")
    public ResponseEntity<InputStreamResource> downloadImages(@PathVariable String orderId) {
        return iMediaImageService.userDownloadImages(orderId);
    }

    @GetMapping("/WXDownloadImage/{orderId}/{token}")
    public ResponseEntity<InputStreamResource> WXDownloadImages(@PathVariable String orderId,@PathVariable String token) {

        return iMediaImageService.userWXDownloadImages(orderId,token);
    }

}
